﻿using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CompanyManagement
{
    public class Utility
    {

        public static bool checkCookie()
        {
            if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string getFullPath()
        {
            return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
        }

        public static string getUserName()
        {
            if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
            {
                return HttpContext.Current.Request.Cookies["UserCookie"].Values["zUserName"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public static string getRolename()
        {
            if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
            {
                return HttpContext.Current.Request.Cookies["UserCookie"].Values["zRoleName"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public static string getLastLoginDate()
        {
            if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
            {
                return HttpContext.Current.Request.Cookies["UserCookie"].Values["LastLoginDate"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public static string getProfilePic()
        {
            if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
            {
                if (HttpContext.Current.Request.Cookies["UserCookie"].Values["ProfilePicName"] == null)
                    return string.Empty;
                else
                    return HttpContext.Current.Request.Cookies["UserCookie"].Values["ProfilePicName"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public static int getRoleID()
        {
            if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
            {
                return Convert.ToInt32(HttpContext.Current.Request.Cookies["UserCookie"].Values["zRoleID"].ToString());
            }
            else
            {
                return 0;
            }
        }

        public static int getUserID()
        {
            int userid = 0;
            if (HttpContext.Current.Request.Cookies["UserCookie"] != null)
            {
                userid = Convert.ToInt32(HttpContext.Current.Request.Cookies["UserCookie"].Values["zUserID"].ToString());
            }
            return userid;
        }

        public static void setCookiValue()
        {
            UserDLL dllUser = new UserDLL();
            UserMetaData usermetadata = dllUser.GetUserByID(Utility.getUserID());
            HttpCookie aCookie = HttpContext.Current.Request.Cookies["UserCookie"];
            aCookie.Values["zUserID"] = Convert.ToString(usermetadata.UserID);
            aCookie.Values["zRoleID"] = Convert.ToString(usermetadata.RoleID);
            aCookie.Values["zUserName"] = Convert.ToString(usermetadata.UserName);
            aCookie.Values["zPassword"] = Convert.ToString(usermetadata.Password);
            aCookie.Values["zRoleName"] = Convert.ToString(new RoleDLL().GetRoleByID(usermetadata.RoleID ?? 0).RoleName);
            aCookie.Values["ProfilePicName"] = Convert.ToString(usermetadata.ProfilePicPath);
        }

        public static string GetProjectUsers(int projectid)
        {
            string users = string.Empty;
            var userslist = new CompanyManagementDLL.DLL.ProjectAssignDLL().getProjectAssignUserListByProjectID(projectid);
            int i = 0;
            foreach (var user in userslist)
            {
                users += user.UserName;
                if (i > 0 && i < userslist.Count - 1)
                {
                    users += ", ";
                }
            }
            return users;
        }
    }
}